
package com.yimixian.app.adapter;

import com.yimixian.app.cart.CartManager;
import com.yimixian.app.cart.CartNewFreebieGoodsItemView;
import com.yimixian.app.cart.CartNewGoodsItemView;
import com.yimixian.app.cart.GoodsItemPresenter;
import com.yimixian.app.common.CacheKeys;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.model.AppConfig;
import com.yimixian.app.model.GoodsItem;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 购物车模块ListView需要的Adapter Created by songzhiyang on 2015/8/19.
 */
public class CartItemAdapter extends BaseAdapter {

    private Context mContext;
    public List<GoodsItem> mGoodsItems;
    private CartManager mCartManager;// 目前的各种数据源
    private Fragment mFragment;// 存放该View所在的Fragment
    private GoodsItemPresenter.OnPlusButtonClickListener mPlusButtonListener;

    public CartItemAdapter(Context context, GoodsItemPresenter.OnPlusButtonClickListener listener,
            Fragment fragment) {
        this.mContext = context;
        this.mPlusButtonListener = listener;
        this.mFragment = fragment;

        // 初始化购物车数据
        mCartManager = CartManager.getInstance();

        mGoodsItems = new ArrayList<>();
        updateGoodsIds();
    }

    public void updateGoodsIds() {
        mGoodsItems.clear();
        for(int i=0;i<mCartManager.mGoodsGroup.size();i++) {
            for(int j=0;j<mCartManager.mGoodsGroup.get(i).goods.size();j++) {
                mGoodsItems.add(mCartManager.mGoodsGroup.get(i).goods.get(j));
            }
        }
    }

    @Override
    public int getCount() {
        return mGoodsItems.size();
    }

    @Override
    public GoodsItem getItem(int position) {
        return mGoodsItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        int positionSize = 0;
        int goodsGroupPosition = 0;
        boolean isNormalView = true;
        if(position == 0) {
            isNormalView = false;
        } else {
            for (int i = 0; i < mCartManager.mGoodsGroup.size(); i++) {
                positionSize += mCartManager.mGoodsGroup.get(i).goods.size();
                if (position == positionSize) {
                    isNormalView = false;
                    goodsGroupPosition = i + 1;
                    break;
                }
            }
        }
        View view = null;
        view = getFreebieView(position, convertView, isNormalView,goodsGroupPosition);
        return view;
    }

    // 获取普通商品的view
    public View getNormalView(int position, View convertView) {
        if (convertView == null) {
            convertView = new CartNewGoodsItemView(mContext, this);
        }
        CartNewGoodsItemView itemView = (CartNewGoodsItemView) convertView;
//        itemView.setFragment(mFragment);
        itemView.bind(getItem(position));
        // 设置点 "+" Button按钮的动画监听事件
        // itemView.setOnPlusButtonClickListener(mPlusButtonListener);
        itemView.setShowDividerBelow(position < getCount() - (mCartManager.mHasFreebie ? 2 : 1));
        itemView.setBackgroundColor(Color.WHITE);

        return itemView;
    }

    // 获取特殊商品的view
    private View getFreebieView(int position, View convertView,boolean isNarmalView,int goodsGroupPosition) {
        String title = "";
//        if (convertView == null) {
        if(goodsGroupPosition < mCartManager.mGoodsGroup.size()) {
            title = mCartManager.mGoodsGroup.get(goodsGroupPosition).title;
        }
            convertView = new CartNewFreebieGoodsItemView(mContext, TextUtils.isEmpty(title) ? ""
                    : title, this);
//        }
        CartNewFreebieGoodsItemView itemView = (CartNewFreebieGoodsItemView) convertView;
        itemView.bind(getItem(position));
        // 设置点 "+" Button按钮的动画监听事件
        //itemView.setOnPlusButtonClickListener(mPlusButtonListener);
        itemView.setShowDividerBelow(false);
        itemView.setBackgroundColor(Color.WHITE);
        if(isNarmalView) {
            itemView.hideTitleView();
        }
        if(TextUtils.isEmpty(title)) {
            itemView.hideTitleView();
        }
        if(!getItem(position).isActivated) {
            itemView.showFLMasking();
        } else {
            itemView.hideFLMasking();
        }
        if(TextUtils.isEmpty(getItem(position).tips)) {
            itemView.hideTextTips();
        } else {
            itemView.setTextTips(getItem(position).tips);
            itemView.showTextTips();
        }
        return itemView;
    }

//    @Override
//    public int getViewTypeCount() {
//        return 2;
//    }
//
//    @Override
//    public int getItemViewType(int position) {
//        if (getItem(position) != null) {
//            return getItem(position).typeGift == null ? 0 : 1;
//        } else {
//            return 0;
//        }
//    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    // @Override
    // public boolean isEnabled(int position) {
    // return false;
    // }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

}
